package cn.lr.org.controller;

import cn.lr.basic.Exception.BusinessException;
import cn.lr.basic.util.AjaxResult;
import cn.lr.basic.util.ExcelUtil;
import cn.lr.basic.util.PageList;
import cn.lr.basic.vo.ShopVo;
import cn.lr.org.domain.Shop;
import cn.lr.org.domain.ShopAuditLog;
import cn.lr.org.query.ShopQuery;
import cn.lr.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;

    @GetMapping("/echart")
    public Map<String,Object> echart(){
        List<ShopVo> shopVos = shopService.loadCount();
        List<Integer> states = new ArrayList<>();
        List<Long> nums = new ArrayList<>();
        for (ShopVo shopVo : shopVos) {
            states.add(shopVo.getState());
            nums.add(shopVo.getNum());
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("x",states);
        map.put("y",nums);
        return map;
    }
    /**
     * 导出数据
     * @param response
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response){
        try {
            List<Shop> shops = shopService.loadAll();
            ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class,"shop.xlsx", response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入数据
     * @param file
     */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file){
        try {
            List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
            shopService.batchAdd(shops);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/audit/pass")
     public AjaxResult pass(@RequestBody ShopAuditLog log){
        try {
            shopService.pass(log);
            return AjaxResult.me();
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    @GetMapping("/audit/{id}")
     public AjaxResult pass2(@PathVariable("id") Long id){
        try {
            shopService.updateState(id);
            return AjaxResult.me();
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    @PostMapping("/audit/reject")
     public AjaxResult reject(@RequestBody ShopAuditLog log){
        try {
            shopService.reject(log);
            return AjaxResult.me();
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    @PostMapping("/audit/refuse")
     public AjaxResult refuse(@RequestBody ShopAuditLog log){
        try {
            shopService.refuse(log);
            return AjaxResult.me();
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e){ // 自定义异常
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    /**
     * 查询单个--restful风格
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.loadById(id);
    }

    /**
     * 查询所有--restful风格
     * @return
     */
    @GetMapping
    public List<Shop> queryAll(){
        return shopService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id) {
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
           return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            shopService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
           return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    @PostMapping("/list")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop) {
        try {
            if(shop.getId() != null){
                shopService.update(shop);
            } else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

}
